unit uExportDecl_form_functions;

interface
     uses  SysUtils, Classes, Forms, Windows, Graphics, Controls, StdCtrls,
     ExtCtrls, ComCtrls, spin, Menus , Dialogs, XPMan
     ;


type

  control_size_t = packed record
       width  :integer;
       height :Integer;
   end;


  function ProcessMessages() :Boolean; stdcall;

 	function CreateForm(
		ppOut :PPointer;
		const caption :PAnsiChar;
		const name :PAnsiChar;
		const recScreen :PRect
		) : HRESULT; stdcall;

  procedure ShowForm(pForm :Pointer); stdcall;


  function ShowFormModal(pform :pointer) : integer; stdcall;

  function CloseForm(pform : pointer) :HRESULT; stdcall;

  function GetFormMainMenu(pform :pointer) :pointer; stdcall;

  function SearchForm(const formName :PAnsiChar) :pointer; stdcall;
  procedure DestroyForm(ppForm :PPointer); stdcall;

  function IsFormHasMainMenu(pForm :pointer) :Boolean; stdcall;

  function GetFormClientSize(pForm :pointer) :control_size_t; stdcall;

  function CreateGroupBox(
		ppOut :PPointer;
		const caption :PAnsiChar;
		owner :Pointer;
		parent :Pointer;
		const recParent :PRECT
		) : HRESULT; stdcall;


  function CreateButton(
		ppOut :PPointer;
		const caption :PAnsiChar;
		owner :pointer;
		parent :pointer;
		const recOnTheParent :PRECT) : HRESULT; stdcall;


  function CreateCheckBox(
		ppOut :PPOinter;
		owner :pointer;
		parent :pointer;
		const caption :PAnsichar;
    x :Integer;
    y :Integer;
		checked : boolean
		) : HRESULT; stdcall;



  	function CreateLabel(
      ppOut :PPointer;
      owner  :Pointer;
      parent :Pointer;

      const caption :PAnsiChar;
      bold :Boolean;

      x :integer;
      y :Integer

      ) : HRESULT; stdcall;


  function CreateRadioItems (
		ppOut  :PPOINTER;
		owner  :POINTER;
		parent :POINTER;

    const caption :PAnsiChar;
    
		const items :PAnsiChar;
		selectedIndex :integer;

		x :integer;
		y :integer

		) :HRESULT; stdcall;


    function CreateEdit(
			ppOut :PPointer;
			owner :pointer;
			parent :pointer;

			x :integer;
			y :integer;
			width :integer;

			const text :PAnsiChar
		) : HRESULT; stdcall;


    function CreateSlider(
		ppOut :PPointer;
		owner :pointer;
		parent :pointer;

		x :integer;
		y :integer;
		len  :integer;
		vertical_Or_Horizontal :boolean;

		min_pos :integer;
		max_pos :integer;
		posit :integer

		) : HRESULT; stdcall;

    function CreateMemo(
      ppOut  :ppointer;
      owner  :pointer;
      parent :pointer;
      const recOnTheParent :PRECT;
      const text :PAnsiCHar
      ) :HRESULT; stdcall;

  function CreateListBox(
		ppOut :PPointer;
		owner :Pointer;
		parent :pointer;
		const recOnTheParent :PRECT;
		const items :PAnsiChar;
		select_index :integer ) :HRESULT; stdcall;


  function CreateUpDown(
		ppOut  :ppointer;
		owner  :pointer;
		parent :pointer;
		const recOnTheParent :PRECT;

		orientVerticalOrHorizontal :boolean

		) :HRESULT; stdcall;


 function CreateSpinEdit(
      ppOut :PPointer;
      owner :pointer;
      parent :Pointer;

      x :integer;
      y :integer;
      width  :integer

	) : HRESULT; stdcall;


 	function CreateComboBox (
		ppOut  :ppointer;
		owner  :pointer;
		parent :pointer;

		const items  : PAnsiChar;
		selected_item : integer;

		x  : integer;
		y  : integer;
		width : integer
		) :HRESULT; stdcall;


  function CreatePageControl(
		ppOut :ppointer;
		owner :pointer;
		parent :pointer;

		const recOnTheParent :PRECT
		) :HRESULT; stdcall;

  function CreatePageControlTabSheet(
	    pPageControl :pointer; const caption :PansiChar) :pointer; stdcall;

  function CreatePopupMenu(owner :Pointer; const name :PAnsiChar) :Pointer; stdcall;

  function CreatePopupMenuItem(pPopupMenu :Pointer; const parent_item_caption :PAnsiChar;
      const caption :PAnsiChar) :Pointer; stdcall;

  function GetFormWindowState(pForm :pointer) :TWindowState;

  function GetFormBorderStyle(pForm :pointer) : TFormBorderStyle ; stdcall;
	procedure SetFormBorderStyle(pForm :Pointer; bs :TFormBorderStyle); stdcall;

  function GetNumForms() :integer; stdcall;

  procedure setFormIconFromFile(pForm :Pointer;
      const filename :PAnsiChar); stdcall;

function CreatePanel(owner :pointer; parent :pointer; const recOnTheParent :PRECT) :pointer; stdcall;

function CreateTreeView(owner :pointer; parent :pointer;
      const recOnTheParent: PRECT) :pointer; stdcall;

function CreateTreeViewItem(pTreeView  :PAnsiChar;
    const parent_item_caption :PAnsiChar; const caption :PAnsiChar; data :pointer) :Pointer; stdcall;


function CreateProgressBar( owner :Pointer; parent :Pointer; const  recOnTheParent :PRECT	) :pointer; stdcall;

function CreateSpliter(owner :pointer; parent :pointer; width :integer) :Pointer; stdcall;

function CreateStatusBar(owner :pointer; parent :pointer) :Pointer; stdcall;
function CreateStatusBarPanel(pStatusBar :Pointer; const text :PAnsiChar;
        width :integer):Pointer; stdcall;


procedure PopupMenuPopup(pPopupMenu :pointer; x :Integer; y :integer); stdcall;

procedure SetFormStyle(pForm :pointer; fs :TFormStyle); stdcall;

function GetApplication() :Pointer; stdcall;


//{$R *.dfm}
//{$R WindowsXP.res}

function GetFormDeviceContext(pForm :pointer) :HDC; stdcall;
function GetFormCanvas(pFOrm :pointer) :Pointer; stdcall;


function search_node(const tn :TTreeNode;  const text :AnsiString) :TTreeNode; overload;
function search_node(const ns :TTreeNodes; const text :AnsiString) :TTreeNode; overload;


  
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation  //--------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
     uses uExportGuiEventCallbackpas ;


function ProcessMessages() :Boolean; stdcall;
  var i :Integer;
begin

  Application.ProcessMessages();
  Result := g_helper.process_messages_result;
//  Result := Application.Terminated;
  //result := Application.MainForm.Visible;


  i := 0;
end;


function CreateForm(
		ppOut :PPointer;
		const caption :PAnsiChar;
		const name :PAnsiChar;
		const recScreen :PRect ) : HRESULT; stdcall;

  var newform :TForm;

  //xpmnfst: TXPManifest;

begin
  newform := nil;
  result := E_FAIL;

  Application.CreateForm(TForm, newform);


  //xpmnfst := TXPManifest.Create(newform);


  // copy data
  newform.Caption := caption;
  newform.Name := name;
  //newform.Color := clWhite;

    newform.Top := recScreen^.Top;
    newform.Left := recScreen^.Left;
    newform.Width := recScreen^.Right - recScreen^.Left;
    newform.Height := recScreen^.Bottom - recScreen^.Top;


    // assign events
    newform.OnResize :=  g_helper.OnFormResize;
    newform.OnClose :=  g_helper.OnFormClose;
    newform.OnCloseQuery := g_helper.OnFormCloseQuery;
    newform.OnClick := g_helper.OnFormClick;
    newform.OnDestroy := g_helper.OnFormDestroy;
    newform.OnPaint  := g_helper.OnFormPaint;

    newform.OnActivate :=   g_helper.OnFormActivate ;
    newform.OnShow :=   g_helper.OnFormShow ;
    newform.OnHide :=   g_helper.OnFormHide ;

    newform.OnMouseDown := g_helper.OnFormMouseDown;
    newform.OnMouseMove := g_helper.OnFormMouseMove;
    newform.OnMouseUp := g_helper.OnFormMouseUp;
    
    

    //ShowMessage('need a more events ...');



  // copy to out
  ppOut^ := Pointer(newform);





  Result := 0;
end;

procedure ShowForm (pForm :Pointer); stdcall;
  var n :TForm;
begin
  n := TForm( pForm );
  n.Show();

end;

function ShowFormModal(pform :pointer) : integer; stdcall;
  var frm :TForm;
begin
    frm := TForm(pform);
    Result := -1;
    result := frm.ShowModal();

end;

function CloseForm(pform : pointer) :HRESULT; stdcall;
  var frm : TForm;
begin
  frm := TForm(pform);
  frm.Close();
  Result := 0;

end;

function GetFormClientSize(pForm :pointer) :control_size_t; stdcall;
  var frm :TForm;
begin
  frm := TForm(pForm);

  Result.width  := 0;
  Result.height := 0;

  Result.width  := frm.ClientWidth;
  Result.height := frm.ClientHeight;

end;

function GetFormMainMenu(pform :pointer) :pointer; stdcall;
  var frm : TForm;
   mm : TMainMenu;
begin
  result := nil;
  frm := TForm( pform );
  mm := frm.Menu;
  result := Pointer( mm );
end;

function CreateGroupBox(
		ppOut :PPointer;
		const caption :PAnsiChar;
		owner :Pointer;
		parent :Pointer;
		const recParent :PRECT     ) : HRESULT; stdcall;

    var grp :TGroupBox;
     own :TComponent;
begin
  own :=  TComponent(owner);
    grp := TGroupBox.create(own);
    grp.Parent := TWinControl(parent);

    // assign data
    grp.Caption := caption;

    grp.Top := recParent^.Top;
    grp.Left := recParent^.Left;
    grp.Width := recParent^.Right - recParent^.Left;
    grp.Height := recParent^.Bottom - recParent^.Top;

    // events
    //grp.on

    

    ppOut^ := Pointer(grp);



   // SHowMessage('no code');

    result := 0;
end;

function SearchForm(const formName :PAnsiChar) :pointer; stdcall;
  var frm : TForm;
  cm : TComponent;
  c : Integer;
begin
  result := nil;

  for c := c to Application.ComponentCount do
   begin
      cm := Application.Components[c];
      if cm.ClassName = 'TForm' then
        begin
          frm := TForm(cm);
          if frm.Name = formName then
            begin
              result := Pointer(frm);
              Exit;
            end;

        end;

   end;


end;

procedure DestroyForm(ppForm :PPointer); stdcall;
  var frm :TForm;
  ptr :pointer;
  fnd :TComponent;
begin
  if ppForm = nil
    then Exit;

  ptr := ppForm^;

  if ptr = nil then Exit;


  frm := TForm( ptr );
  fnd := Application.FindComponent(frm.Name);
  if fnd <> nil then
    begin
      Application.RemoveComponent(fnd);
    end;

  FreeAndNil(frm);
  ppForm^ := nil;

end;

function IsFormHasMainMenu(pForm :pointer) :Boolean; stdcall;
  var frm :TForm;
begin
  result := false;
  frm := TForm( pForm );
  if frm.Menu <> nil then
    begin
      Result :=  True;
      Exit;
    end;


end;

function CreateButton(
		ppOut :PPointer;
		const caption :PAnsiChar;
		owner :pointer;
		parent :pointer;
		const recOnTheParent :PRECT) : HRESULT; stdcall;

  var btn :TButton;
begin
  btn := nil;
  btn := TButton.Create( TComponent(owner) );
  btn.Parent := TWinControl(parent);
  btn.OnClick := g_helper.OnButtonCLick;

  // set data
  btn.Caption := caption;
    btn.Top := recOnTheParent^.Top;
    btn.Left := recOnTheParent^.Left;
    btn.Width := recOnTheParent^.Right - recOnTheParent^.Left;
    btn.Height := recOnTheParent^.Bottom - recOnTheParent^.Top;
    btn.Visible := True;

    ppOut^ := Pointer(btn);


    Result := 0;
end;


function CreateCheckBox(
		ppOut :PPOinter;
		owner :pointer;
		parent :pointer;
		const caption :PAnsichar;
    x :Integer;
    y :Integer;
		checked : boolean
		) : HRESULT; stdcall;

    var n :TCheckBox;
begin
  n := TCheckBox.Create( TComponent(owner) );
  n.Parent := TWinControl( parent );
  n.Caption := caption;
  n.Left := x;
  n.Top := y;
  n.Checked := checked;
  n.OnClick := g_helper.OnCheckBoxClick;


  ppOut^ := Pointer(n);


  result := 0;
end;



function CreateLabel(
      ppOut :PPointer;
      owner  :Pointer;
      parent :Pointer;

      const caption :PAnsiChar;
      bold :Boolean;

      x :integer;
      y :Integer

      ) : HRESULT; stdcall;

  var n :TLabel;
  var fs :TFontStyles;
begin
  n := TLabel.Create( TComponent(owner) );
  n.Parent := TWinControl(parent);

  n.Caption := caption;
  if(bold) then
  begin
    fs := n.Font.Style;
    fs := [fsBold]	;
    n.Font.Style := fs;

  end;

  n.Left := x;
  n.Top := y;

  ppOut^ := Pointer(n);


   Result := 0;
end;


function CreateRadioItems (
		ppOut  :PPOINTER;
		owner  :POINTER;
		parent :POINTER;

    const caption :PAnsiChar;

		const items :PAnsiChar;
		selectedIndex :integer;

		x :integer;
		y :integer

		) :HRESULT; stdcall;

    var n :TRadioGroup;
    sl :TStringList;
begin
  n := TRadioGroup.create(  TComponent(owner)  );
  n.Parent := TWinControl(parent);
  n.OnClick := g_helper.OnRadioGroupClick;

    n.Caption := caption;
    n.Left := x;
    n.Top := y;

    // assign items
    sl := TStringList.create();
    sl.Text := items;
    n.Items.Text := sl.Text;
    sl.Free();



    ppOut^ := Pointer(n);

  //ShowMessage('');

  result := 0;
end;


function CreateEdit(
			ppOut :PPointer;
			owner :pointer;
			parent :pointer;

			x :integer;
			y :integer;
			width :integer;

			const text :PAnsiChar
		) : HRESULT; stdcall;

    var n :TEdit;
begin
    n := TEdit.Create(  TComponent(owner)  );
    n.Parent := TWinControl( parent );
    n.OnChange := g_helper.OnEditChange;




    // assign data
    n.Left := x;
    n.Top  := y;
    n.Width := width;
    n.Text := text;

    ppOut^ := Pointer(n);

    result := 0;
end;


function CreateSlider(
		ppOut :PPointer;
		owner :pointer;
		parent :pointer;

		x :integer;
		y :integer;
		len  :integer;
		vertical_Or_Horizontal :boolean;

		min_pos :integer;
		max_pos :integer;
		posit :integer

		) : HRESULT; stdcall;

    var n :TTrackBar;

begin

   n := TTrackBar.Create( TComponent(owner) );
   n.Parent := TWinControl( parent );
   n.OnChange := g_helper.OnTrackBarChange;

   n.Left := x;
   n.Top  := y;

   // type TTrackBarOrientation = (trHorizontal, trVertical;

   if(vertical_Or_Horizontal)
    then
      begin
         // vert
         n.Orientation := trVertical;
         n.Width := 22;
         n.Height := len;


      end
    else
      begin
        // horiz
        n.Orientation := trHorizontal;
        n.Height := 22;
        n.Width := len;


      end;


   n.Min := min_pos;
   n.Max := max_pos;
   n.Position := posit;
   

    ppOut^ := Pointer(n);

    result := 0;
end;


function CreateMemo(
    ppOut  :ppointer;
    owner  :pointer;
    parent :pointer;
    const recOnTheParent :PRECT;
    const text :PAnsiCHar
    ) :HRESULT; stdcall;
 var n :TMemo;
  ss :TScrollStyle;
begin
  result := E_FAIL;
   n := TMemo.Create( TComponent(owner) );
   n.Parent := TWinControl( parent );
   n.OnChange := g_helper.OnMemoChange;

   ss := n.ScrollBars;
   // type TScrollStyle = (ssNone, ssHorizontal, ssVertical, ssBoth);
   ss :=  ssBoth;
   n.ScrollBars := ss;


   n.text := text;

    n.Top := recOnTheParent^.Top;
    n.Left := recOnTheParent^.Left;
    n.Width := recOnTheParent^.Right - recOnTheParent^.Left;
    n.Height := recOnTheParent^.Bottom - recOnTheParent^.Top;
   // btn.Visible := True;

    ppOut^ := Pointer(n);


    Result := 0;


end;


function CreateListBox(
		ppOut :PPointer;
		owner :Pointer;
		parent :pointer;
		const recOnTheParent :PRECT;
		const items :PAnsiChar;
		select_index :integer ) :HRESULT; stdcall;

    var n :TListBox;
    var sl :TStringList;
begin
   result := E_FAIL;
   n := TListBox.Create( TComponent(owner) );
   n.Parent := TWinControl( parent );
   n.OnClick  := g_helper.OnListBoxClick;

    sl := TStringList.Create;
    sl.Text := items;
    n.Items := sl;
    sl.Free;

    n.Top := recOnTheParent^.Top;
    n.Left := recOnTheParent^.Left;
    n.Width := recOnTheParent^.Right - recOnTheParent^.Left;
    n.Height := recOnTheParent^.Bottom - recOnTheParent^.Top;

    n.ItemIndex := select_index;

    ppOut^ := Pointer(n);
    result := 0;
end;



function CreateUpDown(
		ppOut  :ppointer;
		owner  :pointer;
		parent :pointer;
		const recOnTheParent :PRECT;

		orientVerticalOrHorizontal :boolean

		) : HRESULT; stdcall;
   var n :TUpDown;
begin
    result := E_FAIL;
   n := TUpDown.Create( TComponent(owner) );
   n.Parent := TWinControl( parent );
   n.OnClick := g_helper.OnUpDownClick;

   if(orientVerticalOrHorizontal)  // type TUDOrientation = (udHorizontal, udVertical);
   then
      begin
       n.Orientation :=  udVertical;


      end
   else
     begin
      n.Orientation :=  udHorizontal;

     end;

    n.Top := recOnTheParent^.Top;
    n.Left := recOnTheParent^.Left;
    n.Width := recOnTheParent^.Right - recOnTheParent^.Left;
    n.Height := recOnTheParent^.Bottom - recOnTheParent^.Top;


    ppOut^ := Pointer(n);
    result := 0;
end;


 function CreateSpinEdit(
      ppOut :PPointer;
      owner :pointer;
      parent :Pointer;

      x :integer;
      y :integer;
      width  :integer

	) : HRESULT; stdcall;

  var n :TSpinEdit;
begin
   result := E_FAIL;
   n := TSpinEdit.create( TComponent(owner) );
   n.Parent := TWinControl( parent );
   n.OnChange := g_helper.OnSpinEditChange;

   n.Left := x;
   n.Top := y;
   n.Width := width;


   ppOut^ := Pointer(n);
   result := 0;
end;


function CreateComboBox (
		ppOut  :ppointer;
		owner  :pointer;
		parent :pointer;

		const items  : PAnsiChar;
		selected_item : integer;

		x  : integer;
		y  : integer;
		width : integer
		) :HRESULT; stdcall;


    var n : TComboBox;
    var sl : TStringList;
begin
  result := E_FAIL;

   n := TComboBox.create( TComponent(owner) );
   n.Parent := TWinControl( parent );
   n.OnChange := g_helper.OnComboBoxChange;

   n.Left := x;
   n.Top := y;
   n.Width := width;


    sl := TStringList.Create;
    sl.Text := items;
    n.Items := sl;
    sl.Free;

    n.ItemIndex := selected_item;

    // type TComboBoxStyle =
    // (csDropDown, csSimple, csDropDownList, csOwnerDrawFixed, csOwnerDrawVariable);
   n.Style := csDropDownList;

   ppOut^ := Pointer(n);
   result := 0;
end;


function CreatePageControl(
		ppOut :ppointer;
		owner :pointer;
		parent :pointer;

		const recOnTheParent :PRECT
		) :HRESULT; stdcall;

    var n : TPageControl;
begin
   result := E_FAIL;

   n := TPageControl.create( TComponent(owner) );
   n.Parent := TWinControl( parent );
   n.OnChange := g_helper.OnPageControlChange;


    n.Top := recOnTheParent^.Top;
    n.Left := recOnTheParent^.Left;
    n.Width := recOnTheParent^.Right - recOnTheParent^.Left;
    n.Height := recOnTheParent^.Bottom - recOnTheParent^.Top;

   ppOut^ := Pointer(n);
   result := 0;
end;

function CreatePageControlTabSheet(
	    pPageControl :pointer; const caption :PansiChar) :pointer; stdcall;
var pc :TPageControl;
   ts : TTabSHeet;
begin
   result := nil;
   pc := TPageControl(pPageControl);

   ts := TTabSHeet.Create( pc );
   ts.Parent := pc;
   ts.Caption := caption;
   ts.PageControl := pc;

   Result := Pointer(ts);

end;

function CreatePopupMenu(owner :Pointer; const name :PAnsiChar) :Pointer; stdcall;
  var ppm :TPopupMenu;
  oc :TComponent;
begin
  result := nil;
  oc := TComponent( owner );
  ppm := TPopupMenu.Create( oc );
  ppm.Name := name;

  Result := Pointer( ppm );

end;

function CreatePopupMenuItem(pPopupMenu :Pointer; const parent_item_caption :PAnsiChar;
      const caption :PAnsiChar) :Pointer; stdcall;

 var pm :TPopupMenu;
 par :TMenuItem;
 
 var n :TMenuItem;

begin
   result := nil;
   pm := TPopupMenu(pPopupMenu);
   n := TMenuItem.create(pm);
   n.OnClick := g_helper.OnPopupMenuItemClick;


   if(parent_item_caption = nil)
    then
      begin
        n.Caption := caption;
        pm.Items.Add(n);
        Result := Pointer(n);
        Exit;
      end;

       par := pm.Items.Find(parent_item_caption);

   if(par = nil)
    then
      begin
          // error !
          result := nil;
          Exit;
      end;


    n.Caption := caption;
    par.Add(n);
    Result := Pointer(n);


end;


function GetFormWindowState(pForm :pointer) :TWindowState;
  var f : TForm;
begin
  f := TForm( pForm );
  result := TWindowState(0);
  Result := f.WindowState;

end;

function GetFormBorderStyle(pForm :pointer) : TFormBorderStyle ; stdcall;
 var frm : TForm;
begin
  frm := TForm(pForm);
  Result := frm.BorderStyle;


end;

procedure SetFormBorderStyle(pForm :Pointer; bs :TFormBorderStyle); stdcall;
 var frm : TForm;
begin
  frm := TForm(pForm);
  frm.BorderStyle := bs;

end;

function GetNumForms() :integer; stdcall;
  var frm : TForm;
  cnt : TComponent;
  var c : Integer;
begin
  Result := 0;

 // Application.ComponentCount

  for c := 0 to Application.ComponentCount do
    begin
      cnt := TComponent( Application.Components[c] );
      if cnt.ClassName = 'TForm' then
        begin
          Result := result + 1;
        end;
    end;


end;

procedure setFormIconFromFile(pForm :Pointer;
      const filename :PAnsiChar); stdcall;

  var frm :TForm;
  fns : AnsiString;
begin
  frm := TForm( pForm );
  fns := filename;
  frm.Icon.LoadFromFile( fns );


end;

procedure PopupMenuPopup(pPopupMenu :pointer; x :Integer; y :integer); stdcall;
 var pm :TPopupMenu;
begin
  pm := TPopupMenu(pPopupMenu);
  pm.Popup(x,y);

end;

function CreatePanel(owner :pointer; parent :pointer; const recOnTheParent :PRECT) :pointer; stdcall;
  var n :TPanel;
begin
    Result := nil;
    n := TPanel.Create( TComponent(owner) );
    n.Parent := TWinControl(parent);

    n.Top    := recOnTheParent^.Top;
    n.Left   := recOnTheParent^.Left;
    n.Width  := recOnTheParent^.Right  - recOnTheParent^.Left;
    n.Height := recOnTheParent^.Bottom - recOnTheParent^.Top;

    result := Pointer( n );

end;

function CreateTreeView(owner :pointer; parent :pointer; const recOnTheParent: PRECT) :pointer; stdcall;
  var tv :TTreeView;
begin
  result := nil;
  tv := TTreeView.Create( TComponent(owner) );
  tv.Parent := TWinControl( parent );
  
  tv.OnChange := g_helper.OnTeeViewChange;
  tv.OnClick  := g_helper.OnTeeViewClick;
  tv.OnCollapsed := g_helper.OnTeeViewCollapsed;
  tv.OnExpanded  := g_helper.OnTeeViewExpanded;

  
   
   tv.Top := recOnTheParent^.Top;
   tv.Left := recOnTheParent^.Left;
   tv.Width := recOnTheParent^.Right - recOnTheParent^.Left;
   tv.Height := recOnTheParent^.Bottom - recOnTheParent^.Top;


  result := Pointer(tv);
end;


function CreateSpliter(owner :pointer; parent :pointer; width :integer) :Pointer; stdcall;
  var n :TSplitter;
begin
  result := nil;
  n := TSplitter.Create( TComponent(owner) );
  n.Parent := TWinControl( parent );
  n.Width := width;

  Result := Pointer(n);

end;


function search_node(const tn :TTreeNode; const text :AnsiString) :TTreeNode; overload;
  var c :Integer; newnode :TTreeNode;
begin
  Result := nil;
  newnode := nil;
  for c := 0 to tn.count-1 do
    begin
        newnode := tn.Item[c];
        if newnode.Text = text then
          begin
            result := newnode;
            Exit;
          end;

        if tn.HasChildren then
          begin
            Result := search_node(newnode, text);
            if Result <> nil
               then Exit;
          end;
    end;


end;


function search_node(const ns :TTreeNodes; const text :AnsiString) :TTreeNode; overload;
  var c :integer;
   node :TTreeNode;
begin
    result := nil;

    for c := 0 to ns.count-1 do
     begin
       node := ns.Item[c];

       if node.Text = text then
          begin
             Result := node;
             Exit;
          end;


         Result := search_node(node, text);
         if Result <> nil then
           begin
             Exit;
           end;
     end;

end;





function CreateTreeViewItem(pTreeView  :PAnsiChar;
    const parent_item_caption :PAnsiChar; const caption :PAnsiChar; data :pointer) :Pointer; stdcall;

 var n :TTreeNode ;
    tv :TTreeView;
    pint_error :PInteger;
begin
   result := nil;
   n := nil;

   tv := TTreeView(pTreeView);
 

   if parent_item_caption <> nil
     then
        begin
          n := search_node( tv.Items, parent_item_caption );

          if n = nil then
            begin
              // do error
              pint_error := nil;
              pint_error^ := 0;

            end;

          n := tv.Items.AddChild(  n, caption );
          n.Data := data;
          result := Pointer(n);          
          Exit;
        end
     else
        begin
          n := tv.Items.AddChild(  nil, caption );
          n.Data := data;
          result := Pointer(n);
          Exit;
        end;


end;



function CreateProgressBar( owner :Pointer; parent :Pointer;
                const  recOnTheParent :PRECT	) :pointer; stdcall;
    var n : TProgressBar;
begin
    Result := nil;
    n := TProgressBar.Create( TComponent(owner) );
    n.Parent := TWinControl(parent);

   n.Top := recOnTheParent^.Top;
   n.Left := recOnTheParent^.Left;
   n.Width := recOnTheParent^.Right - recOnTheParent^.Left;
   n.Height := recOnTheParent^.Bottom - recOnTheParent^.Top;

    result := Pointer( n );
    


end;


function CreateStatusBar(owner :pointer; parent :pointer) :Pointer; stdcall;
  var n :TStatusBar;
begin
  result := nil;
  n := TStatusBar.Create(owner);
  n.Parent := TWinControl(parent);
  Result := Pointer(n);

end;


function CreateStatusBarPanel(pStatusBar :Pointer; const text :PAnsiChar;
        width :integer):Pointer; stdcall;
   var sb :TStatusBar;
   sp :TStatusPanel;
begin
  result := nil;
  sb := TStatusBar(pStatusBar);
  sp := sb.Panels.Add();
  sp.Text := text;
  sp.Width := width;

  result := Pointer( sp );

end;


procedure SetFormStyle(pForm :pointer; fs :TFormStyle); stdcall;
  var frm :TForm;
begin
  frm := TForm( pForm );
  frm.FormStyle := fs;

end;

function GetApplication() :Pointer; stdcall;
begin
  Result := Pointer( Application );
end;

function GetFormDeviceContext(pForm :pointer) :HDC; stdcall;
 var frm :TForm;
begin
  result := 0;
  frm := TForm(pForm);
  result := frm.Canvas.Handle;
end;

function GetFormCanvas(pFOrm :pointer) :Pointer; stdcall;
 var frm :TForm;
begin
  result := nil;
  frm := TForm(pForm);
  result := Pointer(frm.Canvas);

end;

end.
